// pages/buy/buy.js
const app = getApp();
var imgId = "";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    /*商圈选择器*/
    busCirList: [],
    buyClassList: [],
    /*分类选择器*/
    index: 0,
    multiArray: [['女装', '男装','童装','太太装'], ['上衣', '毛衣', '连衣裙', '套装，裤']],
    multiIndex: [0, 0],
    /**地址选择器 */
    region: ['浙江省', '杭州市', '江干区'],
    customItem: '全部',
    //textarea.js
    focus: false,
    hint: '请输入手机号码',
    nameHint: '请输入联系人姓名',
    resHint: '请输入备用电话(非必填)',
    countHint: '请输入商品数量',
    srcImg: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.getData({
      _url: app.globalData._url + '/tradeArea/selectList',
      data: '',
    }, function (json) {
      console.log(json.data)
      if (json.data && json.success) {
        that.setData({
          busCirList: json.data,
        })
      }
    })
    that.getData({
      _url: app.globalData._url + '/wantBuyType/selectWantBuyTypeTwoList',
    }, function (json) {
      console.log(json.data)
      if (json.data && json.success) {
        that.setData({
          buyClassList: json.data,
        })
      }
    })
  },
  /*添加样本图*/
  addPhoto() {
    var that = this;
    wx.chooseImage({
      count: 6, // 默认9
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;

        if (that.data.srcImg.length < 3) {
          that.data.srcImg.push({ src: tempFilePaths[0] });
          var _src = that.data.srcImg;
          that.setData({
            srcImg: _src
          });
          wx.uploadFile({
            url: app.globalData.baseUrl + tempFilePaths, //仅为示例，非真实的接口地址
            filePath: tempFilePaths[0],
            name: 'fileMap',
            header: { "Content-Type": "multipart/form-data" },
            success: function (res) {
              imgId = JSON.parse(res.data).value
            },
            fail: function () {
              console.log('上传失败')
            }
          })
        }


      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  /**
   * 事件函数
   */
  /*添加样本图*/ 
  addPhoto() {
    var that = this;
    wx.chooseImage({
      count: 6, // 默认9
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;

        if (that.data.srcImg.length < 3) {
          that.data.srcImg.push({ src: tempFilePaths[0] });
          var _src = that.data.srcImg;
          that.setData({
            srcImg: _src
          });
          wx.uploadFile({
            url: app.globalData.baseUrl + tempFilePaths, //仅为示例，非真实的接口地址
            filePath: tempFilePaths[0],
            name: 'fileMap',
            header: { "Content-Type": "multipart/form-data" },
            success: function (res) {
              imgId = JSON.parse(res.data).value
            },
            fail: function () {
              console.log('上传失败')
            }
          })
        }


      }
    })
  },
  /**选择器 */
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  bindMultiPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      multiIndex: e.detail.value
    })
  },
  bindMultiPickerColumnChange: function (e) {
    console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value;
    switch (e.detail.column) {
      case 0:
        switch (data.multiIndex[0]) {
          case 0:
            data.multiArray[1] = ['上衣', '毛衣', '连衣裙', '套装，裤'];
            break;
          case 1:
            data.multiArray[1] = ['上衣', '毛衣', '套装，裤'];
            break;
          case 2:
            data.multiArray[1] = ['上衣', '毛衣', '连衣裙', '套装，裤'];
            break;
          case 3:
            data.multiArray[1] = ['上衣', '毛衣', '套装，裤'];
            break;
        }
        data.multiIndex[1] = 0;
        break;
      case 1:
        switch (data.multiIndex[0]) {
        }
        console.log(data.multiIndex);
        break;
    }
    this.setData(data);
  },
  /*商圈选择器*/ 
  bindbusCirChange: function (e) {
    this.setData({
      index: e.detail.value,
    })
  },
  /*分类选择器*/
  proClass: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value,
    })
  },
  
  /* 手机号输入框 */ 
  bindInputBlur: function (e) {
    var PhoneReg = new RegExp('^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$');
    var phones = PhoneReg.exec(e.detail.value);
    if (phones){
      this.setData({
        result: e.detail.value,
        hint: '请输入手机号'
      })
    } else {
      this.setData({
        result: '',
        hint: '手机号有误，请重新输入'
      })
    }
  },
  /*备用电话*/ 
  resPhone: function (e) {
    if (e.detail.value != ''){
      var PhoneReg = new RegExp('^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$');
      var phones = PhoneReg.exec(e.detail.value);
      if (phones) {
        this.setData({
          resResult: e.detail.value,
        })
      } else {
        this.setData({
          resResult: '',
          resHint: '输入有误，可重新输入'
        })
      }
    } else{
      this.setData({
        resResult: '',
      })
    }
    
  },
  /*姓名输入框 */
  phoneCodeCheck: function (e) {
    this.setData({
      cliName: e.detail.value
    })
  },
  /*数量输入框*/
  proCount: function (e) {
    var conutReg = new RegExp('[1-9]');
    var pro_count = conutReg.exec(e.detail.value);
    if (pro_count){
      if (e.detail.value <= 99999 && e.detail.value > 0){
        this.setData({
          countResult: e.detail.value,
        })
      } else if (e.detail.value && e.detail.value > 99999) {
        this.setData({
          countResult: 99999,
        })
      } 
    } else {
      this.setData({
        countResult: '',
        countHint: '商品数量输入有误'
      })
    }
  },
    /*地址选择器*/ 
  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  },
  /**商品描述文本框 */
  bindButtonTap: function () {
    this.setData({
      focus: true,
    })
  },
  bindTextAreaBlur: function (e) {
    this.setData({
      descri: e.detail.value,
    })
  },
  getData: function (params, cb) {
    wx.request({
      url: params._url,
      data: params.data,
      method: 'GET',
      header: {
        'content-type': 'application/json;charset=UTF-8'
      },
      success: function (res) {
        if (res.statusCode == 200) {
          typeof cb === 'function' && cb(res.data)
        }
      },
      fail: function () {
      },
      complete: function () {
      }
    })
  },
  formSubmit: function (e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    
    const pub_buscir = e.detail.value['pub-buscir'];
    const cli_name = e.detail.value['cliName'];
    const client_phone = e.detail.value['clientPhone'];
    const addre = e.detail.value['address'];
    const pro_class = e.detail.value['proClass']
    wx.request({
      url: '',
      method: "post",
      params: {
        'pub_buscir': pub_buscir,
      },
      success: function(json){},
      fail: function(json){}
    })
  },
})